package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.AddCircleBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.fragement.HotsPostFragment;
import com.hangzhou.welbeing.welbeinginstrument.fragement.NewPostFragment;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 08.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 真实的圈子
 */
public class TrueCircleActivity extends FragmentActivity {


    private static final String TAG = "TrueCircleActivity";
    @Bind(R.id.activity_true_circle_iv_return)
    ImageView activityTrueCircleIvReturn;
    @Bind(R.id.activity_true_circle_rl_iv_head_portrait)
    ImageView activityTrueCircleRlIvHeadPortrait;
    @Bind(R.id.activity_true_circle_rl_tv_concrete_circle)
    TextView activityTrueCircleRlTvConcreteCircle;
    @Bind(R.id.activity_true_circle_tv_members)
    TextView activityTrueCircleTvMembers;
    @Bind(R.id.item_popular_circle_recyclerview_tv_join)
    TextView activityTrueCircleTvJoin;
    @Bind(R.id.activity_true_circle_rl_iv_add)
    ImageView activityTrueCircleRlIvAdd;
    @Bind(R.id.activity_true_circle_tv_topic)
    TextView activityTrueCircleTvTopic;
    @Bind(R.id.activity_true_circle_tv_topic_content)
    TextView activityTrueCircleTvTopicContent;
    @Bind(R.id.activity_true_circle_ll_topic)
    LinearLayout activityTrueCircleLlTopic;
    @Bind(R.id.activity_true_circle_tv_action)
    TextView activityTrueCircleTvAction;
    @Bind(R.id.activity_true_circle_tv_action_content)
    TextView activityTrueCircleTvActionContent;
    @Bind(R.id.activity_true_circle_ll_action)
    LinearLayout activityTrueCircleLlAction;
    @Bind(R.id.activity_true_circle_tv_news)
    TextView activityTrueCircleTvNews;
    @Bind(R.id.activity_true_circle_rl_news)
    RelativeLayout activityTrueCircleRlNews;
    @Bind(R.id.activity_true_circle_tv_hots)
    TextView activityTrueCircleTvHots;
    @Bind(R.id.activity_true_circle_rl_hots)
    RelativeLayout activityTrueCircleRlHots;
    @Bind(R.id.activity_true_circle_fl_post)
    FrameLayout activityTrueCircleFlPost;


    @Bind(R.id.activity_true_circle_floatingActionButton)
    FloatingActionButton activityTrueCircleFloatingActionButton;


    private NewPostFragment newPostFragment;
    private HotsPostFragment hotsPostFragment;
    private String circle_id;
    private String circle_num;
    private String circle_name;
    private String circle_state;
    private String circle_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_true_circle);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);

        circle_id = getIntent().getStringExtra("circle_id");
        circle_num = getIntent().getStringExtra("circle_num");
        circle_name = getIntent().getStringExtra("circle_name");
        circle_state = getIntent().getStringExtra("circle_state");
        circle_url = getIntent().getStringExtra("circle_url");
      //  activityTrueCircleFloatingActionButton.setBackgroundTintMode(PorterDuff.Mode.SRC_OVER);
        Glide.with(this).load(RBConstants.SERVER_URL+"/Public/Uploads/"+ circle_url).transform(new GlideCircleTransform(this)).into(activityTrueCircleRlIvHeadPortrait);
        activityTrueCircleRlTvConcreteCircle.setText(circle_name);
        activityTrueCircleTvMembers.setText("圈成员 "+ circle_num);
        if(circle_state.equals("0")){
            activityTrueCircleTvJoin.setText("加入");
        }else if(circle_state.equals("1")){
            activityTrueCircleTvJoin.setText("退出");
        }

        rlNews();
    }


    @OnClick({R.id.activity_true_circle_iv_return, R.id.activity_true_circle_rl_iv_add, R.id.activity_true_circle_ll_topic,
            R.id.activity_true_circle_ll_action, R.id.activity_true_circle_rl_news, R.id.activity_true_circle_rl_hots,
            R.id.activity_true_circle_floatingActionButton,R.id.item_popular_circle_recyclerview_tv_join})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_true_circle_iv_return:
                finish();
                break;
            case R.id.activity_true_circle_rl_iv_add:
                break;
            case R.id.activity_true_circle_ll_topic:
                break;
            case R.id.activity_true_circle_ll_action:
                break;
            case R.id.activity_true_circle_rl_news:  //最新
                activityTrueCircleTvNews.setTextColor(0xffFFB3B3); //红
                activityTrueCircleTvHots.setTextColor(0xff000000);  //黑
                rlNews();
                break;
            case R.id.activity_true_circle_rl_hots:  //最热
                activityTrueCircleTvNews.setTextColor(0xff000000); //黑
                activityTrueCircleTvHots.setTextColor(0xffFFB3B3);//红
                rlHots();
                break;
            case R.id.activity_true_circle_floatingActionButton:
                Intent intent = new Intent(TrueCircleActivity.this, EditingActivity.class);
                intent.putExtra("circle_id",circle_id);
                intent.putExtra("circle_name",circle_name);
                intent.putExtra("circle_num",circle_num);
                intent.putExtra("circle_state",circle_state);
                intent.putExtra("circle_url",circle_url);
                startActivity(intent);
                finish();
                break;
            case R.id.item_popular_circle_recyclerview_tv_join:
                String tv_join = activityTrueCircleTvJoin.getText().toString().trim();
                if(tv_join.equals("加入")){
                    activityTrueCircleTvJoin.setText("退出");
                    addCircle("1");
                }else if(tv_join.equals("退出")){
                    activityTrueCircleTvJoin.setText("加入");
                    cancel("1");
                }

            break;
        }
    }
    private void addCircle(String id) {
        SharedPreferences sp = MyApplication.getMcontext().getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.ADD_CIRCLE)
                .addParams("cat_id", id)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final AddCircleBean addCircleBean = gson.fromJson(praiseParseNetworkResponse,AddCircleBean.class);
                        if(addCircleBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),"添加成功",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),addCircleBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 取消
     * @param id*/
    private void cancel(String id) {
        SharedPreferences sp = MyApplication.getMcontext().getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.CANCEL_JOIN)
                .addParams("cat_id",id)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final AddCircleBean addCircleBean = gson.fromJson(praiseParseNetworkResponse,AddCircleBean.class);
                        if(addCircleBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),"已取消",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),addCircleBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 最新帖子  /Home/CusPostsCat/lst 我的圈子
     * */
    private void rlNews() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        /**
         * 为了防止重叠，需要点击之前先移除其他Fragment
         */
        if(newPostFragment != null){
            transaction.remove(newPostFragment);
        }
        newPostFragment = new NewPostFragment();
        transaction.replace(R.id.activity_true_circle_fl_post, newPostFragment);
        transaction.commit();
    }
    /**
     * @desc 最热帖子
     * */
    private void rlHots() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        /**
         * 为了防止重叠，需要点击之前先移除其他Fragmentj
         */
        if(hotsPostFragment != null){
            transaction.remove(hotsPostFragment);
        }
        hotsPostFragment = new HotsPostFragment();
        transaction.replace(R.id.activity_true_circle_fl_post, hotsPostFragment);
        transaction.commit();
    }
}
